using System;
using System.Collections.Generic;
using Core;
using DoItNow.Features;
using EnvDTE;
using EnvDTE80;
using Extensibility;

namespace DoItNow
{
	public class Connect : IDTExtensibility2, IDTCommandTarget
	{
		private Ide _ide;
		private AddIn _addIn;
		private CommandRegistrar _commandRegistrar;
	    private OutputPane _outputPane;

		private readonly List<Feature> _features = new List<Feature>();

		public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
		{
            _ide = new Ide((DTE2)application);
            _addIn = (AddIn)addInInst;
            _commandRegistrar = new CommandRegistrar("DoItNow.Connect.", _ide.Commands, _ide.FindCommandBar("Amnesia"), _addIn);
            _outputPane = new OutputPane(_ide.CreateOutputPane("DoItNow"));

            _features.Add(new OpenFileFeature(_ide, _outputPane));
            _features.Add(new OpenTestFeature(_ide, _outputPane));
            _features.Add(new ToggleBetweenHeaderAndCppFeature(_ide, _outputPane));
            _features.Add(new ToggleBetweenImplementationAndTestFeature(_ide, _outputPane));
            _features.Add(new NextCamelCaseWordFeature(_ide, _outputPane));
            _features.Add(new NextCamelCaseWordExtendFeature(_ide, _outputPane));
            _features.Add(new PreviousCamelCaseWordFeature(_ide, _outputPane));
            _features.Add(new PreviousCamelCaseWordExtendFeature(_ide, _outputPane));

            foreach (Feature feature in _features)
            {
                var bindableFeature = feature as BindableCommandFeature;

                if (bindableFeature == null)
                    continue;

                _commandRegistrar.Register(bindableFeature);
            }
        }

		public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
		{
		}

		public void OnAddInsUpdate(ref Array custom)
		{
		}

		public void OnStartupComplete(ref Array custom)
		{
		}

		public void OnBeginShutdown(ref Array custom)
		{
		}

		public void Exec(string commandName, vsCommandExecOption executeOption, ref object variantIn, ref object variantOut, ref bool handled)
		{
			handled = _commandRegistrar.Exec(commandName.Replace(_commandRegistrar.Prefix, ""));
		}

		public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus statusOption, ref object commandText)
		{
			if (neededText != vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
				return;

			statusOption = _commandRegistrar.QueryStatus(commandName.Replace(_commandRegistrar.Prefix, ""));
		}
	}
}